package com.harmony.pilot.data.controller;

import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.common.dto.ASTSymbol;
import com.harmony.pilot.data.dto.*;
import com.harmony.pilot.data.service.CodeQLQueryManager;
import com.harmony.pilot.data.service.EnhancedCodeQLService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CodeQL增强控制器
 * 提供CodeQL AST查询能力
 */
@Slf4j
@RestController
@RequestMapping("/data/codeql")
@RequiredArgsConstructor
public class CodeQLController {
    
    private final EnhancedCodeQLService enhancedCodeQLService;
    private final CodeQLQueryManager queryManager;
    
    /**
     * 执行自定义查询
     */
    @PostMapping("/query")
    public RestResult<CodeQLResult> executeQuery(@RequestBody CodeQLQueryRequest request) {
        log.info("Executing CodeQL query: {}", request.getQueryFile());
        
        try {
            CodeQLResult result = enhancedCodeQLService.executeQuery(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to execute CodeQL query", e);
            return RestResult.error("Failed to execute query: " + e.getMessage());
        }
    }
    
    /**
     * 查询符号（AST增强版）
     */
    @PostMapping("/symbols/ast")
    public RestResult<List<ASTSymbol>> querySymbolsAST(@RequestBody SymbolQueryRequest request) {
        log.info("Querying symbols with AST: symbol={}", request.getSymbol());
        
        try {
            List<ASTSymbol> symbols = enhancedCodeQLService.querySymbolsAST(request);
            return RestResult.success(symbols);
        } catch (Exception e) {
            log.error("Failed to query symbols with AST", e);
            return RestResult.error("Failed to query symbols: " + e.getMessage());
        }
    }
    
    /**
     * 获取可用查询列表
     */
    @GetMapping("/queries")
    public RestResult<List<CodeQLQueryManager.QueryInfo>> listQueries(
            @RequestParam(required = false) String kind) {
        log.info("Listing CodeQL queries: kind={}", kind);
        
        try {
            List<CodeQLQueryManager.QueryInfo> queries;
            if (kind != null && !kind.isEmpty()) {
                queries = queryManager.listQueriesByKind(kind);
            } else {
                queries = queryManager.listQueries();
            }
            return RestResult.success(queries);
        } catch (Exception e) {
            log.error("Failed to list queries", e);
            return RestResult.error("Failed to list queries: " + e.getMessage());
        }
    }
    
    /**
     * 获取查询信息
     */
    @GetMapping("/queries/{queryId}")
    public RestResult<CodeQLQueryManager.QueryInfo> getQueryInfo(@PathVariable String queryId) {
        log.info("Getting query info: {}", queryId);
        
        try {
            CodeQLQueryManager.QueryInfo info = queryManager.getQueryInfo(queryId);
            if (info == null) {
                return RestResult.error("Query not found: " + queryId);
            }
            return RestResult.success(info);
        } catch (Exception e) {
            log.error("Failed to get query info", e);
            return RestResult.error("Failed to get query info: " + e.getMessage());
        }
    }
    
    /**
     * 重新加载查询文件
     */
    @PostMapping("/queries/reload")
    public RestResult<String> reloadQueries() {
        log.info("Reloading CodeQL queries");
        
        try {
            queryManager.reload();
            return RestResult.success("Queries reloaded successfully");
        } catch (Exception e) {
            log.error("Failed to reload queries", e);
            return RestResult.error("Failed to reload queries: " + e.getMessage());
        }
    }
}

